﻿using LS.Infrastructure.ControllerExtension;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;
using QInfrastructure.Models.QueryCondition;
using QInfrastructure.Interfaces.SystemManager;
using LS.Infrastructure.ExceptionExtension;
using Microsoft.AspNetCore.Authorization;

namespace QInfrastructure.WebCore.Areas.SystemManager;



[Area("SystemManager")]
public class SystemUserController : QBaseController
{
    public IActionResult Index()
    {
        return View();
    }










    #region  登录页逻辑代码
    [Route("/Login")]
    public IActionResult Login(string returnUrl)
    {
        ViewData["ReturnUrl"] = returnUrl;
        return View();
    }

    [AllowAnonymous]
    public IActionResult LoginSubmit(SystemUserDto systemUser)
    {
        var res = GetService<TSystemUser>().LoginSubmit(systemUser);
        if (res.userId == Guid.Empty)
        {
            throw new BusinessException("用户登录失败,用户名/密码不正确");
        }
        var claims = new List<Claim>()
        { 
            new Claim(ClaimTypes.Name, res.usercode),
            new Claim(ClaimTypes.NameIdentifier, res.userId.ToString())
           
        };

        var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);


        HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
            new ClaimsPrincipal(claimsIdentity)
            );
        //处理
        return Success(systemUser);
    }

	[Route("/Logout")]
	public IActionResult Logout()
    {
        HttpContext.SignOutAsync();
        return Ok();
    }

    #endregion





}
